//
//  M4InfosListAlertView.swift
//  M4
//
//  Created by YuChuang on 2024/8/12.
//

import UIKit

class M4InfosListAlertView: UIView, UITableViewDelegate, UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return M4CellData.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "M4InfosListCell", for: indexPath) as? M4InfosListCell
        cell?.data = M4CellData[indexPath.row]
        cell?.M4InfosCellView.backgroundColor = indexPath.row == M4SelectedIndex ? COLOR_00479D : COLOR_EEEEEE
        cell?.M4InfosCellText.textColor = indexPath.row == M4SelectedIndex ? .white : COLOR_333333
        return cell ?? UITableViewCell()
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        M4SelectedIndex = indexPath.row
        M4SelectedStr = M4CellData[indexPath.row].GTG ?? ""
        M4InfosListTable.reloadData()
        M4SelectedInfoReturn?(M4SelectedStr)
        M4AmountReturn?(M4CellData[indexPath.row])
        M4PopViewHidden(subview: self.M4InfosListBGView)
    }
    
    
    @IBOutlet weak var M4InfosListBGView: UIView!
    
    @IBOutlet weak var M4InfosListTitle: UILabel!
    
    @IBOutlet weak var M4InfosListTable: UITableView!
    
    @IBOutlet weak var M4InfosListViewHeight: NSLayoutConstraint!
    
    var M4CellData: [M4InfoItemListModel] = []
    
    private var M4SelectedStr: String = ""
    
    var M4SelectedIndex: Int? {
        didSet {
            M4SelectedStr = M4CellData[M4SelectedIndex ?? 0].GTG ?? ""
        }
    }
    
    var M4SelectedInfoReturn: ((String) -> Void)?
    
    var M4AmountReturn: ((M4InfoItemListModel) -> Void)?
    
    override func awakeFromNib() {
        self.frame = CGRect(x: 0, y: 0, width: M4ScreenWidth, height: M4ScreenHeight)
        M4InfosListViewHeight.constant = M4ScreenHeight/3*2
        M4InfosListTable.rowHeight = UITableView.automaticDimension
        M4InfosListTable.estimatedRowHeight = 160
        if #available(iOS 15.0, *) {
            M4InfosListTable.sectionHeaderTopPadding = 0
        }
        M4InfosListTable.delegate = self
        M4InfosListTable.dataSource = self
        M4InfosListTable.register(UINib(nibName: "M4InfosListCell", bundle: nil), forCellReuseIdentifier: "M4InfosListCell")
    }
    
    @IBAction func M4InfosListCloseAlert(_ sender: UIButton) {
        self.M4PopViewHidden(subview: self.M4InfosListBGView)
        sender.M4EventUnavailable = false
    }
    
    func M4InfosListUpdateUI(title: String, data: [M4InfoItemListModel]) {
        M4InfosListTitle.text = title
        M4CellData = data
        M4InfosListTable.reloadData()
    }

}

extension M4InfosListAlertView {
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        guard let touch = touches.first else { return }
        let touchePoint = touch.location(in: self)
        if !self.M4InfosListBGView.frame.contains(touchePoint) {
            self.M4PopViewHidden(subview: self.M4InfosListBGView)
        }
    }
    
}
